package io.renren.utils.httpclient;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    /**
     * 判断是否与给定字符串样式匹配
     *
     * @param str     字符串
     * @param pattern 正则表达式样式
     * @return 是否匹配是true, 否false
     */
    public static boolean isMatch(String str, String pattern) {
        Pattern pattern_hand = Pattern.compile(pattern);
        Matcher matcher_hand = pattern_hand.matcher(str);
        boolean b = matcher_hand.matches();
        return b;
    }

    /**
     * 判断是否是空字符串 null和"" null返回result,否则返回字符串
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        if (s != null && !"".equals(s)) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否是null,"",或者空白字符串"  "及制表符
     *
     * @param s
     * @return
     */
    public static boolean isBlank(String s) {
        if (org.apache.commons.lang.StringUtils.isBlank(s)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNotEmpty(String str) {
        if (str != null && !"".equals(str))
            return true;
        else
            return false;
    }


    /**
     * 判断是否是null,"",或者空白字符串"  "及制表符
     *
     * @param s
     * @return
     */
    public static boolean isNotBlank(String s) {
        if (org.apache.commons.lang.StringUtils.isNotBlank(s)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param str
     * @param length
     * @return String    返回类型
     * @throws
     * @Title: cutStringWithRight
     * @Description:截取字符串最后n位
     * @author liujp
     * @date 2016年11月2日 下午5:45:32
     */
    public static String cutStringWithRight(String str, int length) {
        if (str == null)
            return null;
        else if (length >= str.length())
            return str;
        else
            return str.substring(str.length() - length);
    }

    public static boolean isFirstMoreOrEqualsSecond(String first, String second) {
        return first.compareTo(second) >= 0;
    }
    
    public static String strcat(String first, String link, String seconed)
    {
    	return StringUtils.isEmpty(first)?seconed:(StringUtils.isEmpty(seconed)?first:first + link + seconed);
    }
    
    /**
	 * 字段串 s是否在字段串strList中

	 */
	static public boolean isStrIn(String strList, String s) {
		if(StringUtils.isEmpty(strList) || StringUtils.isEmpty(s))
			return false;
		return (("," + strList + ",").indexOf("," + s + ",") >= 0);
	}

	static public boolean isStrIn(String strList, String s, char c) {
		if(StringUtils.isEmpty(strList) || StringUtils.isEmpty(s))
			return false;
		return ((c + strList + c).indexOf(c + s + c) >= 0);
	}
	public static String tranScoding(String s) throws UnsupportedEncodingException{
		
		String ut_utf8Toiso = new String(s.getBytes("ISO8859-1"),"UTF-8");
		return ut_utf8Toiso;
	}
	
	public static String fixNullStr(String str) {
		return "null".equals(str)?null:str;
	}
}
